package com.springboot.blog.controller;

import com.springboot.blog.beans.*;
import com.springboot.blog.dao.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class PageController {

    @Resource
    private ClassifyDao classifyDao;
    @Resource
    private BlogDao blogDao;
    @Resource
    private UserDao userDao;
    @Resource
    private BlogSetDao blogSetDao;
    @Resource
    private SignInMapper signInMapper;
    @Resource
    private ShopVipDao shopVipDao;


    @RequestMapping("/admin")
    public String alogin(){
            return "admin/login";
    }

    @RequestMapping("/user")
    public String ulogin(HttpServletRequest request,Model model) throws Exception {
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        String  userName = (String) request.getSession().getAttribute("userName");
        SignIn signIn = signInMapper.siginCount(userName);
        User user = userDao.queryByUsername(userName);
        if (userName!=null){
            if (signIn==null){
                model.addAttribute("days",0);
                model.addAttribute("reward",0);
                model.addAttribute("vip","非 VIP");
            }else {
                model.addAttribute("days",signIn.getContinueDays());
                model.addAttribute("reward", signIn.getReward());
                if (signIn.getShopname()==null){
                    model.addAttribute("vip","非 VIP");
                }else {
                    model.addAttribute("vip",signIn.getShopname());
                }
            }
            model.addAttribute("user",user);
            return "user/index";
        }
        return "user/login";
    }

    @RequestMapping("admin/adminregist")
    public String regist(){
        return "admin/regist";
    }

    @RequestMapping("user/userregist")
    public String userregist(Model model){
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        return "user/regist";
    }

    @RequestMapping("user/logout")
    public String logout(HttpServletRequest request,Model model){
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        request.getSession().removeAttribute("userName");
        return "user/login";
    }



    @RequestMapping("/erro.html")
    public String erro(){
        return "erro";
    }

    @RequestMapping("admin/adminlogin")
    public String adminlogin(){
        return "admin/login";
    }

    @RequestMapping("user/userlogin")
    public String userlogin(){
        return "user/login";
    }


    @RequestMapping("admin/adminindex")
    public String adminindex(){
        return "admin/index";
    }

    @RequestMapping("user/userindex")
    public String userindex(HttpServletRequest request,Model model){
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        HttpSession session = request.getSession();
        String  userName = (String) session.getAttribute("userName");
        SignIn signIn = signInMapper.siginCount(userName);
        if (signIn==null){
            model.addAttribute("days",0);
            model.addAttribute("reward",0);
            model.addAttribute("vip","非 VIP");
        }else {
            model.addAttribute("days",signIn.getContinueDays());
            model.addAttribute("reward", signIn.getReward());
            if (signIn.getShopname()==null){
                model.addAttribute("vip","非 VIP");
            }else {
                model.addAttribute("vip",signIn.getShopname());
            }
        }
        try {
            User user = userDao.queryByUsername(userName);
            model.addAttribute("user",user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "user/index";
    }

    @RequestMapping("admin/retrieve")
    public String retrieve(){
        return "admin/retrieve";
    }

    @RequestMapping("user/retrieve")
    public String userretrieve(Model model){
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        return "user/retrieve";
    }

    @RequestMapping("admin/reset")
    public String reset(){
        return "admin/reset";
    }

    @RequestMapping("user/reset")
    public String userreset(Model model){
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        return "user/reset";
    }

    @RequestMapping("admin/role")
    public String role(){
        return "admin/role";
    }

    @RequestMapping("admin/roleform")
    public String roleform(){
        return "admin/roleform";
    }

    @RequestMapping("admin/tags")
    public String tags(){
        return "admin/tags";
    }
    @RequestMapping("admin/tagsform")
    public String tagsform(){
        return "admin/tagsform";
    }

    @RequestMapping("admin/editagsform")
    public String editagsform(){
        return "admin/editagsform";
    }

    @RequestMapping("admin/user/userPwd")
    public String userPwd(){
        return "admin/user/userPwd";
    }
    @RequestMapping("user/pass")
    public String userpass(){
        return "user/password";
    }

    @RequestMapping("user/listUserBlog")
    public String listUserBlog(HttpServletRequest request,Model model){
        String  userName = (String) request.getSession().getAttribute("userName");
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        model.addAttribute("user",userName);
        return "user/listUserBlog";
    }
    @RequestMapping("user/store")
    public String store(Model model){
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        List<BlogShop> blogShops = shopVipDao.selectList(null);
        model.addAttribute("vipshop",blogShops);
        return "user/store";
    }

    @RequestMapping("user/order")
    public String order(Model model){
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        return "user/order";
    }

    @RequestMapping("admin/comment")
    public String comment(){
        return "admin/comment";
    }

    @RequestMapping("admin/editComment")
    public String editComment(){
        return "admin/editComment";
    }

    @RequestMapping("admin/log")
    public String log(){
        return "admin/log";
    }

    @RequestMapping("admin/yqfind")
    public String yqfind(){
        return "admin/youqing";
    }
    @RequestMapping("admin/addyq")
    public String addyq(){
        return "admin/youqingform";
    }

    @RequestMapping("404.html")
    public String error(){
        return "404";
    }

    @RequestMapping("admin/youqingedit")
    public String youqingedit(){
        return "admin/youqingedit";
    }

    @RequestMapping("admin/gonggao")
    public String gonggao(){
        return "admin/gonggao";
    }

    @RequestMapping("admin/addGonggao")
    public String addGonggao(){
        return "admin/addGonggao";
    }
    @RequestMapping("admin/shopVip")
    public String shopVip(Model model){

        return "admin/shopVip";
    }
    @RequestMapping("admin/shopVipform")
    public String shopVipform(){
        return "admin/shopVipform";
    }

    @RequestMapping("admin/editvip")
    public String editvip(){
        return "admin/shopVipedit";
    }

    /**
     * 查询分类
     * @return
     */
    @RequestMapping("admin/addBlogs")
    public String addBlog(Model model){
        List<BlogClassify> blogClassifies = classifyDao.selectList(null);
        model.addAttribute("blogClassifies",blogClassifies);
        return "admin/addBlog";
    }


    /**
     * 前台用户发布文章查询分类
     * @return
     */
    @RequestMapping("user/findUserBlog")
    public String findUserBlog(Model model){
        List<BlogClassify> blogClassifies = classifyDao.selectList(null);
        model.addAttribute("blogClassUser",blogClassifies);
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        return "user/addUserBlog";
    }

    @RequestMapping("admin/listBlog")
    public String listBlog(){
        return "admin/listBlog";
    }

    @RequestMapping("admin/editBlog")
    public String editBlog(Model model,String blogId){
        Blog blog = new Blog();
        blog.setBlogId(Integer.parseInt(blogId));
        Blog blogAll = blogDao.selectById(blogId);
        List<BlogClassify> blogClassifyList = classifyDao.selectList(null);
        model.addAttribute("blogClassifyList",blogClassifyList);
        model.addAttribute("blogAll",blogAll);
        return "admin/editBlog";
    }


    @RequestMapping("user/editBlog")
    public String editBloguser(Model model,String blogId){
        Blog blog = new Blog();
        blog.setBlogId(Integer.parseInt(blogId));
        Blog blogAll = blogDao.selectById(blogId);
        List<BlogClassify> blogClassifyList = classifyDao.selectList(null);
        model.addAttribute("blogClassifyList",blogClassifyList);
        model.addAttribute("blogAll",blogAll);
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        return "user/editBlog";
    }



    @RequestMapping("admin/user/list")
    public String userlist(){
        return "admin/user/userlist";
    }

    //查看前台用户登录返回信息info
    @RequestMapping("user/userInfo")
    public String info(HttpServletRequest request, Model model){
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        String  username = (String) request.getSession().getAttribute("userName");
        try {
            User user = userDao.queryByUsername(username);
            model.addAttribute("users",user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "user/info";
    }

    @RequestMapping("user/charge")
    public String charge(Model model){
        List<BlogSet> blogSets = blogSetDao.selectList(null);
        model.addAttribute("index",blogSets);
        return "user/charge";
    }

}
